<template>
  <div class="view-wrapper">
    <div class="head">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="标题：">
          <el-input placeholder="请输入文章标题" v-model="formInline.title"></el-input>
        </el-form-item>
        <el-form-item label="文章类型：">
          <el-select v-model="formInline.articleTypeId" placeholder="请选择">
            <el-option v-for="item in options" :key="item.id" :label="item.value" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态：">
          <el-select v-model="formInline.status" placeholder="请选择">
            <el-option v-for="item in statusOptions" :key="item.id" :label="item.value" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">查询</el-button>
          <el-button @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="listBox">
      <div class="listBox_head">
        <el-button type="primary" @click="addBuy">新建文章</el-button>
      </div>
      <ex-table ref="table" :loading="loading" :table-column="tableColumn" :table-data="tableData"
        @pageSizeChange="handleSizeChange" @currentPageChange="handleCurrentChange">
        <template v-slot:imgOperate="{ scope }">
          <div>
            <el-image style="width: 150px; height: 100px;margin-right: 10px" :src="scope.row.imgUrl"
              fit="scale-down" :preview-src-list="scope.row.srcList"></el-image>
            <el-image style="width: 150px; height: 100px" :src="scope.row.imgUrl" fit="scale-down"
              :preview-src-list="scope.row.srcList"></el-image>
          </div>
        </template>
        <template v-slot:operate="{ scope }">
          <el-button type="text" size="small" @click="toExamineView(scope.row)">审核</el-button>
          <el-button type="text" size="small" @click="editJump(scope.row.id)">编辑</el-button>
          <el-button type="text" size="small" @click="deleteView(scope.row.id)">删除</el-button>
          <el-button type="text" size="small" @click="commentClick(scope.row)">评论</el-button>
        </template>
      </ex-table>
    </div>
    <detail-dialog :id="dialogId" v-bind="$attrs" v-if="dialogVisible" :visible.sync="dialogVisible"
      @success="_handleSuccess" />
    <!-- 评论弹窗 -->
    <CommentDialog ref="commentDialog" @refresh="getList"></CommentDialog>
    <!-- 审核弹窗 -->
    <ToExamineDialog ref="toExamineDialog" @refresh="getList"></ToExamineDialog>
  </div>
</template>
<script>
  // components
  import {
    kyyArticleList,
    kyyArticleSelect,
    deleteKyyArticle,
  } from "@/api/kyyOfficialWebsite";
  import ExportExcelButton from "@/components/ExportExcelButton/index";
  import DetailDialog from "./components/DetailDialog";
  import CommentDialog from "./components/commentDialog";
  import ToExamineDialog from "./components/toExamineDialog";
  import handlePageMixin from "@/mixins/handlePageMixin";
  import {
    mapGetters
  } from "vuex";
  export default {
    name: "billList",
    components: {
      ExportExcelButton,
      DetailDialog,
      CommentDialog,
      ToExamineDialog,
    },
    mixins: [handlePageMixin],
    data() {
      return {
        loading: false,
        dialogId: '', //编辑id
        formInline: {
          title: "", //标题
          articleTypeId: '', //分类id
          status: '', //文章状态（1已通过 2未通过）
        },
        statusOptions: [{
            id: '',
            value: '全部'
          },
          {
            id: '1',
            value: '已通过'
          },
          {
            id: '2',
            value: '未通过'
          },
        ],
        dialogVisible: false,
        options: [],
        tableData: [],
        tableColumn: [{
            label: "文章分类",
            prop: "articleTypeName",
          },
          {
            label: "文章标题",
            prop: "title",
          },
          {
            label: "更新时间",
            prop: "updateTime",
          },
          {
            label: "评论",
            prop: "commentNum",
          },
          {
            label: "排序",
            prop: "sort",
            width: 120
          },
          {
            label: "审核状态",
            prop: "statusDict",
            width: 150
          },
          {
            label: "操作",
            slot: true,
            slotName: "operate",
            width: 240,
            fixed: "right",
          },
        ],
      };
    },
    computed: {
      ...mapGetters(["isChain"]),
    },
    created() {},
    mounted() {
      this.getType();
      this.getList();
    },
    methods: {
      //获取文章分类
      getType() {
        kyyArticleSelect().then((res) => {
          // var arr = [{
          //   id: "",
          //   value: "全部",
          // }, ];
          // this.options = arr.concat(res.result);
          this.options = res.result;
        });
      },
      getList() { //获取列表
        const params = {
          ...this.pageOptions,
          ...this.formInline,
        };
        this.loading = true;
        kyyArticleList(params)
          .then((res) => {
            const {
              total,
              records
            } = res.result;
            records.forEach((item) => {
              item.operationLoading = false; //显示隐藏按钮Loading
              if (item.status == 1) {
                item.statusDict = '已通过'
              } else if (item.status == 2) {
                item.statusDict = '未通过'
              } else {
                item.statusDict = '-'
              }
            });
            this.tableData = records;
            this.$refs.table.pageOptions.total = total;
          })
          .finally(() => {
            this.loading = false;
          });
      },
      _handleSuccess(e) {
        //成功回调
        this.reset()
      },
      addBuy() {
        //新增
        this.dialogId = null
        this.dialogVisible = true;
      },
      editJump(id) {
        //编辑
        this.dialogId = id
        this.dialogVisible = true;
      },
      deleteView(id) {
        //删除
        this.$confirm("是否删除？", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
          .then(() => {
            deleteKyyArticle(id).then((res) => {
              this.$message({
                message: "删除成功",
                type: "success",
                duration: 1500,
              });
              this.getList();
            });
          })
          .catch(() => {

          });
      },
      search() {
        //搜索
        console.log(this.formInline);
        this.pageOptions.pageNum = 1;
        this.$refs.table.pageOptions.pageNum = 1;
        this.getList();
      },
      reset() {
        this.formInline = {};
        this.pageOptions.pageNum = 1;
        this.$refs.table.pageOptions.pageNum = 1;
        this.getList();
      },
      /** 点击评论 */
      commentClick(row) {
        this.$refs.commentDialog.open(row.id);
      },
      // 审核
      toExamineView(row) {
        this.$refs.toExamineDialog.open(row.id,row.status);
      },
    },
  };

</script>
<style lang="scss" scoped>
  .el-card-title {
    font-size: 20px;
  }

  .head {
    //   box-shadow: 0px 4px 12px 0px rgb(1 41 99 / 6%);
    background-color: #fff;
    border-radius: 8px;
    //   height: 120px;
    padding: 20px 20px 0;
  }

  .el-form--inline .el-form-item {
    margin-right: 40px;
  }

  .listBox {
    //   box-shadow: 0px 4px 12px 0px rgb(1 41 99 / 6%);
    background-color: #fff;
    border-radius: 8px;
    //   height: 120px;
    padding: 20px 20px;
    margin-top: 10px;
  }

  .table_list {
    display: flex;
    align-items: center;
    // border: 1px solid #DCDFE6;
    border-radius: 7px;
    width: 220px;
    margin-bottom: 10px;
  }

  .tableBox {
    width: calc(100% / 3);
    height: 36px;
    text-align: center;
    line-height: 36px;
    color: #303133;
    font-size: 14px;
    border: 1px solid #dcdfe6;
    border-right: 0;
  }

  .tableBox:first-child {
    border-radius: 7px 0px 0px 7px;
  }

  .tableBox:last-child {
    border-right: 1px solid #dcdfe6;
    border-radius: 0px 7px 7px 0px;
  }

  .spchoosetext {
    background-color: #fff4e6;
    color: #ff9300;
    // border: 1px solid #ff9300;
  }

  .listBox_head {
    margin-bottom: 20px;
    display: flex;
  }

  .sumit_button {
    color: #fff !important;
    background: #ff7b35 !important;
    border: 0;
  }

  .sumit_button:hover {
    opacity: 0.9;
  }

  .banner_img {
    width: 200px;
  }

</style>
